﻿using Basic.HrRemoteModel.Emp.Model;
using Basic.OAGatewayModule.ExtendService.Interface;
using Basic.OAGatewayModule.Interface;
using Basic.OAGatewayModule.Model.FillItem;
using Basic.OARemoteModel.FillInItem;
using Basic.OARemoteModel.FillInItem.Model;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.HttpApiGateway.Model;

namespace Basic.OAGatewayModule.lmpl
{
    internal class FillInItemService : IFillInItemService
    {
        private readonly IEmpService _Emp;

        public FillInItemService ( IEmpService emp )
        {
            this._Emp = emp;
        }
        public void Delete ( long fillId, long empId )
        {
            new DeleteFillInEmp
            {
                EmpId = empId,
                FillInId = fillId
            }.Send();
        }
        public void Sync ( long fillId, FillGetEmp obj, long comId )
        {
            EmpBase[] emps = this._Emp.Search(new EmpGetQuery
            {
                CompanyId = comId,
                IsEntry = true,
                UnitId = obj.UnitId,
                DeptId = obj.DeptId,
                Title = obj.Title,
                Post = obj.Post,
                UserType = obj.UserType,
                IsOpenAccount = true
            });
            new SyncFillInEmp
            {
                FillInId = fillId,
                Emps = emps.ConvertAll(a => new FillInEmp
                {
                    CompanyId = a.CompanyId,
                    DeptId = a.DeptId,
                    EmpId = a.EmpId,
                    EmpName = a.EmpName,
                    EmpNo = a.EmpNo,
                    UnitId = a.UnitId
                })
            }.Send();
        }
        public PagingResult<EmpBasicDatum> Query ( PagingParam<FillInItemQuery> param, long comId )
        {
            long[] empId = new QueryFillInEmp
            {
                Size = param.Size,
                SortName = param.SortName,
                Query = param.Query,
                Index = param.Index,
                IsDesc = param.IsDesc,
                NextId = param.NextId
            }.Send(out int count);
            if ( empId.IsNull() )
            {
                return new PagingResult<EmpBasicDatum>(count);
            }
            EmpBasicDatum[] emp = this._Emp.GetBases(comId, empId);
            return new PagingResult<EmpBasicDatum>(emp, count);
        }
    }
}
